package com.crawler.crawler.util;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.UUID;

public class HttpUtils {
    private PoolingHttpClientConnectionManager cm;

    public HttpUtils() {
        this.cm = new PoolingHttpClientConnectionManager();
        //设置最大的连接数
        cm.setMaxTotal(200);

        //设置每个主机的并发数
        cm.setDefaultMaxPerRoute(20);

    }

    //获取内容
    public String getHtml(String url){
        //获取HttpClient对象
        CloseableHttpClient httpClient= HttpClients.custom().setConnectionManager(cm).build();

        //声明httpGet请求对象
        HttpGet httpGet=new HttpGet(url);
        String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";

        httpGet.setHeader("User-Agent",userAgent);
        httpGet.addHeader("Referer", "https://sobooks.cc/");

        httpGet.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");

        httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63");

        httpGet.addHeader("Host","xxxxxxxxx");
        //设置请求参数RequestConfig
        httpGet.setConfig(this.getConfig());
        CloseableHttpResponse response=null;
        try {
            //使用HttpClient发起请求，返回response
            response =httpClient.execute(httpGet);

            //解析response返回的数
            if(response.getStatusLine().getStatusCode()==200){
                String html="";
                //如果response.getEntity获取的结果是空，在执行 EntityUtils.toString 会报错
                //需要对Entity进行非空的判断
                if(response.getEntity()!=null){
                    html= EntityUtils.toString(response.getEntity(),"UTF-8");
                }
                return html;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(response!=null){
                    //关闭连接
                    response.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获取图片
     * @param url
     * @return
     */
    public String getImage(String url){
        //获取httpClient对象
        CloseableHttpClient httpClient=HttpClients.custom().setConnectionManager(cm).build();
        //声明httpGet请求对象
        HttpGet httpGet=new HttpGet(url);
        //设置请求参数RequestConfig
        httpGet.setConfig(this.getConfig());
        CloseableHttpResponse response=null;

        try {
            //使用HttpClient发起请求，返回response
            response =httpClient.execute(httpGet);
            //解析response下载图片
            if(response.getStatusLine().getStatusCode()==200){
                //获取文件类型
                String extName=url.substring(url.indexOf("."));
                //使用uuid生成图片名
                String imageName= UUID.randomUUID().toString()+extName;
                //声明输出的文件
                OutputStream outputStream=new FileOutputStream(new File("D:/E/images/"+imageName));
                //使用响应体输出文件
                response.getEntity().writeTo(outputStream);
                //返回生成的图片名
                return imageName;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(response !=null){
                    //关闭连接
                    response.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    //获取请求参数对象
    private RequestConfig getConfig() {
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)// 设置创建连接的超时时间
                .setConnectionRequestTimeout(500) // 设置获取连接的超时时间
                .setSocketTimeout(10000) // 设置连接的超时时间
                .build();

        return config;
    }

}
